package example.fileupload;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class GRPCServer {
    private Server server;

    public void start(){
        try{

            System.out.println("Starting server...");
            // build gRPC server
            this.server = ServerBuilder.forPort(50096)
                    .addService(new FileUploadService())
                    .build();

            // start
            server.start();

            // shutdown hook
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                System.out.println("gRPC server is shutting down!");
                server.shutdown();
            }));

            server.awaitTermination();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void stop(){
        System.out.println("gRPC server is shutting down!");
        this.server.shutdownNow();
    }
    public static void main(String[] args) throws IOException {
//        GRPCServer grpcServer = new GRPCServer();
//        grpcServer.start();
//        fileclient2 fileclient2 = new fileclient2();
        new Thread(() -> {
            try {
                Thread.sleep(10000);
                fileclient3.send();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();


        GRPCServer SERVER = new GRPCServer();
           Executor EXECUTOR = Executors.newSingleThreadExecutor();
            EXECUTOR.execute(SERVER::start);



    }
}
